import 'package:base/main.dart';
import 'package:flutter/material.dart';
import 'package:framework/data/model/menu_bean.dart';
import 'package:module_home/pages/more_manage/more_manager_logic.dart';

// 域名管理页面
class MoreMenuWidget extends StatelessWidget {
  MoreManagerLogic logic;

  MoreMenuWidget({super.key, required this.logic});

  @override
  Widget build(BuildContext context) {
    return Obx(() => Container(
          padding: EdgeInsets.only(top: 8),
          color: Colors.white,
          child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: logic.menuCategories.entries.map((entry) {
                final category = entry.key;
                final items = entry.value;
                return _buildCategorySection(category, items);
              }).toList()),
        ));
  }

  Widget _buildCategorySection(String title, RxList<Datum> items) {
    if (items.isEmpty) {
      return SizedBox();
    }
    return Container(
      margin: const EdgeInsets.all(8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 分类标题
          Text(title,
              style: const TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.black,
                  fontSize: 14)),
          SizedBox(height: 8),
          Obx(
            () => GridView.builder(
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 5,
                  childAspectRatio: 1.2,
                  crossAxisSpacing: 4,
                  mainAxisSpacing: 4),
              itemCount: items.length,
              itemBuilder: (context, index) {
                final Datum data = items[index];
                return _buildItemView(data, () {
                  logic.addItemData(title, data);
                });
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildItemView(Datum data, final GestureTapCallback? onTap) {
    Widget view = data.edit == true
        ? Icon(Icons.add_circle, color: Colors.blue, size: 16)
        : SizedBox();
    return InkWell(
      onTap: onTap,
      child: Container(
          alignment: Alignment.center,
          child: Stack(
            children: [
              Container(
                padding: EdgeInsets.all(0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ImageLoader.asset(
                        path: 'assets/images/icons/${data.icon}@3x.png',
                        width: 24,
                        height: 24),
                    SizedBox(height: 4),
                    Text(data.name.toString(),
                        style: TextStyle(
                            fontSize: 10, fontWeight: FontWeight.w400)),
                  ],
                ),
              ),
              Align(
                alignment: Alignment.topRight,
                child: view,
              )
            ],
          )),
    );
  }
}
